<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo
 *@link www.zeevin.com
 */
class HomeController extends BaseController{


	public $pageTitle='个人中心_之嗅交友社区';
	
	public function actionIndex(){
		$friends = Yii::app()->db->createCommand()
					->select('u.nickname,u.avatar,f.uid,f.fid,f.status')
					->from('users u,user_followers f')
					->where('(u.id=f.uid and f.fid=:uid) or (u.id=f.fid and f.uid=:uid) ',array('uid'=>$this->uid))
					->queryAll();
		$this->render('index',array('friends'=>$friends));
	}

	public function actionProfile(){
		$model = UserIntro::model()->find('uid=?',array($this->uid))?:(new UserIntro('insert'));

		if(isset($_POST['UserIntro'])){
			$model->attributes = $_POST['UserIntro'];
			if($model->save()){
				$msg['status']=0;
				$msg['desc']='个人介绍设置成功';
			}
		}
		$this->render('profile',array('model'=>$model,'msg'=>$msg));
	}

	public function actionIntro(){

		$split = 'zichash';
		$drop_split='drop_del';
		$intro = Profile::model()->findByAttributes(array('uid'=>$this->uid))?:(new Profile('create'));
		$hook = ProfileHook::model()->findAllByAttributes(array('uid'=>$this->uid))?:(new ProfileHook('create'));
		if(Yii::app()->request->isAjaxRequest){
			// print_r($_POST);exit;
			ProfileHook::model()->deleteAllByAttributes(array('uid'=>$this->uid));
			if(!$intro->isNewRecord)$intro->delete();
			$intro = new Profile('create');
			if(count($_POST)){
				$status = true;
				if(count($_POST['Profile'])){

					$intro->attributes = $_POST['Profile'];
					$status = $intro->save();
				}
				if($status){

					foreach((array)$_POST['Hook'] as $k=>$v){
						$hook = new ProfileHook('create');
						$hook->subject = trim($k);
						$hook->content = trim($v);
						if($v!='drop_del')$hook->save();
					}
				}

				echo $status;exit;

				

			}
		}
		$this->render('intro',array('intro'=>$intro,'hook'=>$hook,'split'=>$split,'drop_split'=>$drop_split));
	}

	public function actionTwitter(){
		$msg['status']=1;
		$msg['desc']='说说信息发布失败';
		if(Yii::app()->request->isAjaxRequest){
			$twitter = new Twitters;
			$twitter->content = trim($_POST['content']);
			if($twitter->save()){
				$link=CHtml::encode(mb_substr($twitter->content,0,20));
				Feed::addfeed(Feed::TYPE_TWITTER, $link);
				$msg['status']=1;
				$msg['desc']='说说信息发布成功';
			}
			
		}
		echo json_encode($msg);
	}

	public function actionFriend(){
		$s=$_GET['s']?$_GET['s']:3;
		$friends = Follower::getList($s);
		// print_r($friends);
		$this->render('friend',array('friends'=>$friends));
	}

	public function actionSet(){
		if(Yii::app()->request->isAjaxRequest){
			$id=$_POST['id'];
			// print_r(json_encode($_POST));exit;
			switch ($_POST['act']){
				case 'refollow': //取消关注
					$msg = Follower::refollow($id);
					break;
				case 'black': //拉黑：先取消关注再加入黑名单
					Follower::refollow($id);
					$msg = Black::addBlack($id);
					break;
				
				default:
					$msg['status']=1;
					$msg['desc']='操作失败';
					break;
			}

			echo json_encode($msg);
		}
	}
	public function actionBlist(){
		if(Yii::app()->request->isAjaxRequest){
			$msg = array('status'=>1,'desc'=>'操作失败');
			if($_POST['id']){
				$rs = Black::model()->findByPk($_POST['id']);
				if($rs->uid==Yii::app()->user->id){
					if($rs->delete()){
						$msg = array('status'=>0,'desc'=>'从黑名单中移除成功');
					}
				}
			}
			echo json_encode($msg);exit;
		}
		$blacks = Black::getList();
		// print_r($blacks);exit();
		$this->render('blist',array('blacks'=>$blacks));
	}
}